﻿using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CozeDomain.IServices;
using D.UtilFreesqlOrm;
using CozeDto.SysDicTypeDtos;
using CozeDomain.Entities;
using CozeDto.SysDicDtos; 

namespace CozeWebApi
{
    /// <summary>
    /// 字典模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class SysDicTypeController : BaseApiController
    {
        private readonly ISysDicService _sysDicService;
        private readonly ISysDicTypeService _sysDicTypeService;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="sysDicService"></param>
        /// <param name="sysDicTypeService"></param>
        public SysDicTypeController(ISysDicService sysDicService, ISysDicTypeService sysDicTypeService)
        {
            _sysDicService = sysDicService;
            _sysDicTypeService = sysDicTypeService;
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<SysDicTypeEntity>>> Pages([FromBody] ReqQuerySysDicTypePage query)
        {
            return await _sysDicTypeService.GetPagesAsync(query);
        }

        /// <summary>
        ///  获取下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepSelect>>> List()
        {
            return await _sysDicTypeService.QueryDicTypeSelect();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddSysDicType input)
        {
            input.CreateUser = Account;
            return await _sysDicTypeService.AddSysDicTypeAsync(input);
        }

        /// <summary>
        /// 单个删除、批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        [UnitOfWork]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",").ToList();
            return await _sysDicTypeService.DeleteSysDicTypeAsync(idsArray);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqUpdateSysDicType"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateSysDicType reqUpdateSysDicType)
        {
            reqUpdateSysDicType.Id = id;
            reqUpdateSysDicType.UpdateUser = Account;
            return await _sysDicTypeService.UpdateSysDicTypeAsync(reqUpdateSysDicType);
        }

        /// <summary>
        /// 根据ID主键获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(template: "{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepSysDicTypeInfo>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = LoginId;
            }
            return await _sysDicTypeService.GetSysDicTypeAsync(id);
        }
    }
}